/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLL;
import com.sun.org.apache.xpath.internal.operations.Equals;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;

/**
 *
 * @author hoangvukenshinPC
 */
public class Client {
  public static void main(String[] args) throws Exception {
    DatagramSocket s = new DatagramSocket();
   // byte[] buf = new byte[]{};
    //DatagramPacket dp = new DatagramPacket(buf, buf.length);
    InetAddress hostAddress = InetAddress.getByName("localhost");
    
    //Receive
    byte[] bufRe = new byte[1024];
    DatagramPacket dgp = new DatagramPacket(bufRe, bufRe.length);
    DatagramSocket sk;
    
    while (true) {
      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      String outMessage = stdin.readLine();

      if (outMessage.equals("bye"))
        break;
      byte[] outByte = {};
      if(outMessage.equals("1"))
      {
          byte[] flag = new byte[]{1};
          flag = "1".getBytes();
          String tenFile = "apologize timbaland";
          byte[] tenFileByte = tenFile.getBytes();
          byte[] buf = new byte[flag.length+tenFileByte.length];
          System.arraycopy(flag, 0, buf, 0, flag.length);
          System.arraycopy(tenFileByte, 0, buf, flag.length, tenFileByte.length);
          DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 4200);
          s.send(out);
      }
      if(outMessage.equals("0"))
      {
      }
      

      s.receive(dgp);
      byte[] reBuf = dgp.getData();
      byte a = reBuf[0];
      byte[] dataB = Arrays.copyOfRange(reBuf, 1, reBuf.length);
      String dataReceive = new String(dataB);
      System.out.println("Nhan flag= " +(char)a + " dữ liệu " + dataReceive);
    }
  }
}
